package com.yanqu.road.dao.impl.union.cross.parergon;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.union.parergon.UnionParergon;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossUnionParergonDaoImpl extends TempDataObjectDao<UnionParergon> {

    public Map<String, UnionParergon> getUnionParergonMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<String, UnionParergon> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_c_union_parergon";
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                UnionParergon data = parse(rs);
                dataMap.put(data.getUnionId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    @Override
    protected boolean addData(UnionParergon data) {
        PreparedStatement call = null;
        String sql = "INSERT INTO t_c_union_parergon (unionId, parergonId, progress, parergonTimes, playerIds, dayRestoreTime) " +
                "VALUES (?, ?, ?, ?, ?, ?)";
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setString(i++, data.getUnionId());
            call.setInt(i++, data.getParergonId());
            call.setInt(i++, data.getProgress());
            call.setInt(i++, data.getParergonTimes());
            call.setString(i++, data.getPlayerIds());
            call.setLong(i++, data.getDayRestoreTime());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UnionParergon data) {
        PreparedStatement call = null;
        String sql = "UPDATE t_c_union_parergon SET parergonId=?, progress=?, parergonTimes=?, playerIds=?, dayRestoreTime=? WHERE unionId=?";
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getParergonId());
            call.setInt(i++, data.getProgress());
            call.setInt(i++, data.getParergonTimes());
            call.setString(i++, data.getPlayerIds());
            call.setLong(i++, data.getDayRestoreTime());
            call.setString(i++, data.getUnionId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UnionParergon parseData(ResultSet rs) throws SQLException {
        UnionParergon data = new UnionParergon();
        data.setUnionId(rs.getString("unionId"));
        data.setParergonId(rs.getInt("parergonId"));
        data.setProgress(rs.getInt("progress"));
        data.setParergonTimes(rs.getInt("parergonTimes"));
        data.setPlayerIds(rs.getString("playerIds"));
        data.setDayRestoreTime(rs.getLong("dayRestoreTime"));
        return data;
    }
}
